package com.loc;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import com.amap.api.location.AMapLocation;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import org.json.JSONObject;

/* compiled from: ReportRecorder.java */
/* loaded from: classes2.dex */
public final class fy {
    private static fy f;
    private static long i;
    private File d;
    private String e;
    private Context g;
    private boolean h;
    private LinkedHashMap<String, Long> c = new LinkedHashMap<>();
    String a = "";
    String b = null;

    private fy(Context context) {
        this.e = null;
        Context applicationContext = context.getApplicationContext();
        this.g = applicationContext;
        String path = applicationContext.getFilesDir().getPath();
        if (this.e == null) {
            this.e = gb.l(this.g);
        }
        try {
            this.d = new File(path, "reportRecorder");
        } catch (Throwable th) {
            ej.a(th);
        }
        c();
    }

    public static synchronized fy a(Context context) {
        fy fyVar;
        synchronized (fy.class) {
            if (f == null) {
                f = new fy(context);
            }
            fyVar = f;
        }
        return fyVar;
    }

    private boolean b(Context context) {
        if (this.b == null) {
            this.b = ga.a(context, "pref", "lastavedate", "0");
        }
        if (this.b.equals(this.a)) {
            return false;
        }
        SharedPreferences.Editor a = ga.a(context, "pref");
        ga.a(a, "lastavedate", this.a);
        ga.a(a);
        this.b = this.a;
        return true;
    }

    private synchronized void c() {
        LinkedHashMap<String, Long> linkedHashMap = this.c;
        if (linkedHashMap == null || linkedHashMap.size() <= 0) {
            this.a = new SimpleDateFormat("yyyyMMdd").format(new Date(System.currentTimeMillis()));
            for (String str : gb.a(this.d)) {
                try {
                    String[] split = new String(fi.b(p.b(str), this.e), "UTF-8").split(",");
                    if (split != null && split.length > 1) {
                        this.c.put(split[0], Long.valueOf(Long.parseLong(split[1])));
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void d() {
        try {
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, Long> entry : this.c.entrySet()) {
                try {
                    String str = entry.getKey() + "," + entry.getValue();
                    sb.append(p.b(fi.a(str.getBytes("UTF-8"), this.e)) + "\n");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            String sb2 = sb.toString();
            if (TextUtils.isEmpty(sb2)) {
                return;
            }
            gb.a(this.d, sb2);
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }

    public final synchronized void a() {
        if (this.h) {
            d();
            this.h = false;
        }
    }

    public final synchronized void a(AMapLocation aMapLocation) {
        try {
            if ((!this.c.containsKey(this.a) && this.c.size() >= 8) || (this.c.containsKey(this.a) && this.c.size() >= 9)) {
                ArrayList<String> arrayList = new ArrayList();
                for (Map.Entry<String, Long> entry : this.c.entrySet()) {
                    arrayList.add(entry.getKey());
                    if (arrayList.size() == this.c.size() - 7) {
                        break;
                    }
                }
                for (String str : arrayList) {
                    this.c.remove(str);
                }
            }
            if (aMapLocation.getErrorCode() != 0) {
                return;
            }
            if (aMapLocation.getLocationType() != 6 && aMapLocation.getLocationType() != 5) {
                if (this.c.containsKey(this.a)) {
                    long longValue = this.c.get(this.a).longValue() + 1;
                    i = longValue;
                    this.c.put(this.a, Long.valueOf(longValue));
                } else {
                    this.c.put(this.a, 1L);
                    i = 1L;
                }
                long j = i;
                if (j != 0 && j % 100 == 0) {
                    a();
                }
                this.h = true;
            }
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }

    public final synchronized void b() {
        try {
            if (b(this.g)) {
                for (Map.Entry<String, Long> entry : this.c.entrySet()) {
                    if (!this.a.equals(entry.getKey())) {
                        JSONObject jSONObject = new JSONObject();
                        jSONObject.put("param_long_first", entry.getKey());
                        jSONObject.put("param_long_second", entry.getValue());
                        fz.a(this.g, "O023", jSONObject);
                    }
                }
            }
        } catch (Throwable th) {
            th.printStackTrace();
        }
    }
}
